package com.example.gbuffer.RenderPass;

import android.content.Context;
import android.opengl.GLES32;

import com.example.gbuffer.Component.Framebuffer.Framebuffer;
import com.example.gbuffer.Component.Model.QuadModel;
import com.example.gbuffer.Component.Shader;

public class ScreenRenderPass extends RenderPass{


    public static QuadModel m_quad = new QuadModel();
    private final static Shader m_quad_shader = new Shader();
    private final static String LOG_TAG = "ScreenPass";
    private static int m_screen_texture_id = 0;

    public ScreenRenderPass(Framebuffer buffer) {
        super(buffer);

        super.addDrawCall(ScreenRenderPass::drawQuad);

        m_quad_shader.setName("LightingShader");
    }

    public static void setScreenTextureId(int textureId) {
        m_screen_texture_id = textureId;
    }

    public void initResources(Context ctx) {

        // 加载模型数据
        m_quad.load();

        // 生成并编译着色器
        m_quad_shader.makeShaderProgramFromAssets(ctx, "screen_pass.vs", "screen_pass.fs");
    }

    private static Integer drawQuad() {

        GLES32.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GLES32.glClear(GLES32.GL_COLOR_BUFFER_BIT);
        GLES32.glDisable(GLES32.GL_DEPTH_TEST);

        GLES32.glActiveTexture(GLES32.GL_TEXTURE0);
        GLES32.glBindTexture(GLES32.GL_TEXTURE_2D, m_screen_texture_id);

        m_quad_shader.use();
        m_quad_shader.setUniform1i("u_screen_tex", 0);

        m_quad.draw(m_quad_shader);

        return 0;
    }
}
